<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : perpetuum.xsl
    Created on : 28. duben 2011, 21:24
    Author     : newohybat newohybat@gmail.com
    Description:
        Stylesheet to transform source data(html) to internal representation (xml - internalConcerts)
-->

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://code.google.com/p/kalkon/internalConcerts">

    <xsl:output method="xml"
                encoding="UTF-8"
                indent="yes"
                media-type="text/xml"/>


<!-- Create concerts  -->
    <xsl:template match="/html">
        <concerts>
            <xsl:attribute name="id"><xsl:text>Perpetuum_Music_Club@KalKon</xsl:text></xsl:attribute>
            <xsl:apply-templates select="//div[((@class='party_incoming-party')or(@class='party_incoming-party2'))and descendant::a[@class='party_incoming-nazev']!='ZAVŘENO / CLOSED' ]"/>
        </concerts>
    </xsl:template>
<!-- Create concert -->
    <xsl:template match="//div[(@class='party_incoming-party')or(@class='party_incoming-party2')]">
        <concert>
            <xsl:apply-templates select="descendant::a[@class='party_incoming-nazev']"/>
            <xsl:apply-templates select="descendant::p[@class='party_incoming-data']"/>
            <location>Perpetuum Music Club,Rooseveltova 9, Brno</location>
            <xsl:apply-templates select="descendant::a[@class='party_incoming-datum']"/>
        </concert>
    </xsl:template>
<!-- Create title -->
    <xsl:template match="//a[@class='party_incoming-nazev']">
        <title><xsl:value-of select="descendant::text()"/></title>
    </xsl:template>
<!--  Create description  -->
    <xsl:template match="//p[@class='party_incoming-data']">
        <xsl:if test="child::a">
            <description><xsl:value-of select="normalize-space(child::a)"/></description>
        </xsl:if>
    </xsl:template>
<!-- Create day_start-->
    <xsl:template match="//a[@class='party_incoming-datum']">
        <xsl:variable name="dateToParse">
            <xsl:choose>
                <xsl:when test="child::span"><xsl:value-of select="substring-after(child::span,' ')"/></xsl:when>
                <xsl:otherwise><xsl:value-of select="substring-after(.,' ')"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="day">
            <xsl:choose>
                <xsl:when test="string-length(substring-before($dateToParse,'.'))!=2">
                    <xsl:value-of select="concat('0',substring-before($dateToParse,'.'))"/>
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="substring-before($dateToParse,'.')"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>    
        <xsl:variable name="month">
            <xsl:choose>
                <xsl:when test="string-length(substring-before(substring-after($dateToParse,'.'),'.'))!=2">
                    <xsl:value-of select="concat('0',substring-before(substring-after($dateToParse,'.'),'.'))"/>
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="substring-before(substring-after($dateToParse,'.'),'.')"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="year" select="substring(substring-after(substring-after($dateToParse,'.'),'.'),1,4)"/>
        <day_start><xsl:value-of select="concat($year,$month,$day)"/></day_start>
    </xsl:template>



</xsl:stylesheet>
